package com.zhn;

//给你一个链表，删除链表的倒数第 n 个结点，并且返回链表的头结点。

public class RemoveNthFromEnd {
    public ListNode removeNthFromEnd(ListNode head,int k){
        ListNode dummyNode = new ListNode(0);
        dummyNode.next = head;
        ListNode fast = dummyNode;
        ListNode slow = dummyNode;
        while (k-- >= 0){
            fast = fast.next;
        }
        while (fast != null) {
            slow = slow.next;
            fast = fast.next;
        }
        slow.next = slow.next.next;
        return dummyNode.next;
    }
}
